package hn.cch.microservice.service_consumer_manual.controller;


import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import hn.cch.microservice.service_consumer_manual.entity.User;
import hn.cch.microservice.service_consumer_manual.feign.client.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.feign.FeignClientsConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

//加载配置文件路径
// @PropertySource(value = {"classpath:application.properties"})
@Import(value = FeignClientsConfiguration.class)
@RestController
public class UserController {

    //读取配置文件参数
    // @Value(value = "${rest.user.url}")
    // private String restUserUrl;

    // @Autowired
    // private RestTemplate restTemplate;

    // @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    public UserController(Client client, Encoder encoder, Decoder decoder, Contract contract) {
        userFeignClient = Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract)
        .requestInterceptor(new BasicAuthRequestInterceptor("username", "password"))
                .target(UserFeignClient.class, "http://service-provider-security/");
    }

    // @Bean
    // public RestTemplate restTemplate() {
    //     RestTemplate restTemplate = new RestTemplate();
    //     return restTemplate;
    // }

    @GetMapping(value = "/{id}")
    public User user(@PathVariable Long id) {
        // return restTemplate.getForObject(restUserUrl + id, User.class);
        return userFeignClient.user(id);
    }

}
